package com.shelpe.services.scheduler.item.keyword;

import java.util.List;

import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import com.shelpe.services.scheduler.mongorepository.KeywordStrTempRepository;
import com.shelpe.services.scheduler.po.KeywordStrTemp;


@Component("insightKeywordReaderTest")
@Scope("step")	
public class InsightKeywordReader implements ItemReader<List<KeywordStrTemp> >{

	private static final int PAGESIZECONST = 70;
	
	@Autowired
	private KeywordStrTempRepository keywordTempRep;
	
    private final Long pageNo;
    
    private final Long pageSize;
    
    private int currentPageNum;
    
    private int beginPageNum;
    
    private int firstPageItem;
    
    private int endPageNum;
  
    private int endPageItemNum;
    
    @Autowired
    public InsightKeywordReader(@Value("#{jobParameters['pageNo']}")final Long pageNo, @Value("#{jobParameters['pageSize']}")final Long pageSize,
    		@Value("#{jobParameters['userId']}")final Long userId){
        
        this.pageNo = pageNo;
        this.pageSize = pageSize;       
        
        int pageNoInt = this.pageNo.intValue();
		int pageSizeInt = this.pageSize.intValue();
		
		int  diffBegin = pageNoInt * pageSizeInt;
		int diffEnd = (pageNoInt + 1) * pageSizeInt;
		
		this.beginPageNum = diffBegin / PAGESIZECONST;
		
		this.firstPageItem = diffBegin % PAGESIZECONST;//第一页跳过的个数
				
		this.endPageNum = diffEnd / PAGESIZECONST;
		
		if(diffEnd % PAGESIZECONST > 0){
			this.endPageNum++;
		}
		
		this.endPageItemNum = diffEnd % PAGESIZECONST;//最后一页要处理的个数
		
		currentPageNum = beginPageNum;	
    }
	
	@Override
	public List<KeywordStrTemp> read()
			throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
		// TODO Auto-generated method stub

		if(currentPageNum < endPageNum){
			Pageable pager = new PageRequest(currentPageNum, PAGESIZECONST);
			Page<KeywordStrTemp> page = keywordTempRep.findAll(pager);
			
			if(page != null && page.getContent() != null && page.getContent().size() > 0){
				
				List<KeywordStrTemp> result = null;
				List<KeywordStrTemp> temp = page.getContent();
				
				if(currentPageNum == beginPageNum && currentPageNum == endPageItemNum){
					
					result = temp.subList(firstPageItem, endPageItemNum);
					
				}else if(currentPageNum == beginPageNum){
					
					int tempSize = temp.size();
					result = temp.subList(firstPageItem, tempSize);
					
				}else if(currentPageNum == endPageItemNum){
					
					result = temp.subList(0, endPageItemNum);
				}else{
					
					result = temp;
				}
				
				currentPageNum++;
				return result;
			}
		}
		
		return null;
	}

}
